<template>
  <div class="main">
    <el-dialog title="新增门店" :visible.sync="showAddSalesroomDialog" center width="770px">
      <el-form :model="salesroomData" :rules="rules" ref="salesroomData">

        <el-form-item label="所属机构" :label-width="formLabelWidth" prop="customer_id">
          <el-select v-model="salesroomData.organization_id" filterable placeholder="请选择">
            <el-option
              v-for="item in organizationList"
              :key="item.customer_organization_id"
              :label="item.name"
              :value="item.customer_organization_id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="门店名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="salesroomData.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="所属省份和城市" :label-width="formLabelWidth">
          <el-cascader placeholder="试试搜索：广东"
                       :options="provinceAndCityList"
                       :props="{ value: 'value', label: 'label'}"
                       @change="handleChange"
                       filterable>
          </el-cascader>
        </el-form-item>


        <el-form-item label="详细地址" :label-width="formLabelWidth">
          <el-input v-model="salesroomData.address" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="联系人" :label-width="formLabelWidth">
          <el-input v-model="salesroomData.contact" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="联系电话" :label-width="formLabelWidth" prop="telephone">
          <el-input v-model="salesroomData.telephone" autocomplete="off"></el-input>
        </el-form-item>

        <!--    prop="head_img"     -->
        <el-form-item :label="'联系人二维码'" :label-width="formLabelWidth">
          <div class="upload_head_img">
            <el-upload :show-file-list="false" :before-upload="beforeAvatarUpload" :http-request="upload_picture" class="avatar-uploader" action="">
              <img v-if="salesroomData.wechat_qrcode" :src="salesroomData.wechat_qrcode" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"/>
            </el-upload>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSubmit">取 消</el-button>
        <el-button type="primary" @click="submitSalesroom('salesroomData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import {add} from '@/api/thk/salesroom'

  import { client } from '@/utils/alioss'

  import {dateFormat} from '@/utils/dateFormat'

  export default {
    name: 'addSalesroom',

    /**
     organization_id            	所属组织
     name                       	机构名称
     country_id                 	区id
     province_id                	省份id
     city_id                     	城市id
     address                    	门店地址
     contact                    	联系人
     wechat_qrcode              	老师二维码
     telephone                  	电话
     */
    data() {
      return {
        showAddSalesroomDialog: false,
        formLabelWidth: '130px',
        salesroomData: {
          organization_id: '',
          name: '',
          province_id: '',
          city_id: '',
          country_id: '',
          address: '',
          contact: '',
          telephone: '',
          wechat_qrcode: '',

        },
        provinceAndCityList: [],
        organizationList: [],



        // 表单校验规则
        rules: {
          // 店铺名为必须
          name:[
            { required: true, message: '请输入机构名称', trigger: 'blur' },
          ],
          telephone:[
            { required: true, message: '请输手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '长度必须为11位', trigger: 'blur' }
          ],
        }
      }
    },



    methods: {
      submitSalesroom(salesroomData){
        // 设置添加的时间
        this.$refs[salesroomData].validate((valid) => {
          console.log('salesroom QwQ data >>>>>>>')
          console.log(this.salesroomData)
          if (valid) {
            add(store.getters.token,this.salesroomData).then(result => {
              // 增加成功
              this.showAddSalesroomDialog = !this.showAddSalesroomDialog
              this.$message.success('增加成功！')
              this.$parent.getSalesroomList()
            }).catch(error => {
              this.$message('增加失败!')
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      cancelSubmit(provinceAndCityList,organizationList){
        this.provinceAndCityList = provinceAndCityList
        console.log('>>>>>>>>>>>>>>')
        this.organizationList = organizationList
        console.log(this.organizationList)
        this.showAddSalesroomDialog = !this.showAddSalesroomDialog
        this.changeCityData()
      },

      // 转换省份数据
      changeCityData(){
        console.log("changeCityList",this.provinceAndCityList)
        let data = {data: this.provinceAndCityList}
        let str = JSON.stringify(data).replace(/province_id/g,'value')
        str = str.replace(/province/g,'label')

        str = str.replace(/city_id/g,'value')
        str = str.replace(/city/g,'label')

        str = str.replace(/country_id/g,'value')
        str = str.replace(/country/g,'label')

        data = JSON.parse(str)
        this.provinceAndCityList = data.data
      },

      handleChange(value){
        if(value){
          this.salesroomData.province_id = value[0]
          this.salesroomData.city_id = value[1]
          this.salesroomData.country_id = value[2]
        }
      },

      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isPNG = file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 1
        if (!isJPG && !isPNG) {
          this.$message.error('上传图片只能是 JPG、PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 1MB!')
        }
        return (isJPG | isPNG) && isLt2M
      },
      upload_picture(file) {
        const that = this
        const fileName = 'user-admin_user-picture' + file.file.uid
        client().put(fileName, file.file).then((res) => {
          this.$set(that.salesroomData, 'wechat_qrcode', res.url)

          console.log(res.url);
          that.$message.success('头像地址上传成功！')
        }).catch((err) => {
          that.$message.error('头像地址上传失败，原因：' + err)
        })
      },

    }
  }
</script>

<style>
  /* 图片上传的样式 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }

  /* 头像地址(head_img)图片上传组件的尺寸 */
  .upload_head_img .avatar-uploader .el-upload{
  }
  .upload_head_img .avatar-uploader-icon{
    font-size: 28px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  .upload_head_img .avatar{
    width: 80px;
    height: 80px;
  }

  .alert-msg{
    position: absolute;
    height: 30px;
    width: 300px;
    top: 10px;
    left: 500px;
  }

</style>
